package com.yinhd.study.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Date;

/**
 * @program: study
 * @description:
 * @Author: chengl
 * @create: 2022-01-21 15:24
 **/

/**
@ResponseBody //表示浏览器接收的不需要跳转
@Controller
*/
@Controller  //可代替上边两个注解
@RequestMapping("/VIID")
public class HelloController {
    private  final Logger logger = LoggerFactory.getLogger(HelloController.class);
    @Autowired
    RestTemplate restTemplate;

    int mark = 0;

    @RequestMapping("/System/Register")
    public String registered(HttpServletRequest request) throws IOException {
        MultiValueMap<String,String> headers = new LinkedMultiValueMap<>();
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        if (StringUtils.hasLength(s)){   //第一次访问401
           if(StringUtils.hasLength(request.getHeader("Authorization"))){
               headers.add("Authenticate", "Digest realm=\"myrealm\", nonce=\"abc123321\"");
               headers.add("Content-Type", "application/*+JSON");
               return headers.toString();
           }else {                      //第二次访问
               headers.add("Content-Type", "application/*+JSON");
               /*
               各种返回信息
                */
               return headers.toString();
           }
        }
        return null;
    }

    @PostMapping("/System/Keepalive")
    public String keepalive(HttpServletRequest request) {
        System.out.println("接收下级心跳请求");
        try {
            BufferedReader reader = request.getReader();
            String str = reader.readLine();
            if (StringUtils.hasLength(str)) {
                MultiValueMap  response = new LinkedMultiValueMap();
                response.put("id","111111111");
                response.put("date",new Date());
                response.put("url","/VIID/System/Keepalive");
                response.put("stutas","O");
                response.put("message","成功响应");
                LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
                headers.add("Content-Type", "application/*+JSON");
                return response.toString();
            }
        } catch (IOException e) {
         logger.info("心跳失败",e);
        }
        return null;
    }

    @PostMapping("/System/Keepalives")   //每隔90s发送一次心跳请求  超过三次确定失联
    public String keepaliveOthers() {
        System.out.println("向上级发送心跳请求");
        try {
            if (mark>3){
                System.out.printf("与上级失去连接");
            }
            MultiValueMap<String,String> headers = new LinkedMultiValueMap<>();
            headers.add("Authenticate", "Digest realm=\"myrealm\", nonce=\"abc123321\"");
            headers.add("Content-Type", "application/*+JSON");
            headers.add("DeviceId","3701241999000001");
            ResponseEntity<MultiValueMap> multiValueMapResponseEntity = restTemplate.postForEntity("localhost:8080/api/user/getById", headers, MultiValueMap.class);
                if (multiValueMapResponseEntity.getStatusCode().equals(0)){
                     mark++;
                }else {
                    System.out.printf("保持连接");
                }
        } catch (Exception e) {
            logger.info("HelloController.java",e);
        }
        return null;
    }


}


